<script setup lang="ts">
interface Blog {
  id: number
  title: string
  excerpt: string
  author: string
  publishDate: string
  tags: string[]
  readTime: number
  views: number
  likes: number
}

interface Props {
  blogs: Blog[]
}

defineProps<Props>()

const emit = defineEmits<{
  selectBlog: [blog: Blog]
}>()
</script>

<template>
  <aside class="sidebar">
    <div class="sidebar-content">
      <div class="search-box">
        <input type="text" placeholder="搜索博客..." class="search-input" />
        <button class="search-btn">🔍</button>
      </div>
      
      <div class="popular-posts">
        <h3>热门文章</h3>
        <div class="post-list">
          <div 
            v-for="blog in blogs.slice(0, 5)" 
            :key="blog.id"
            class="post-item"
            @click="emit('selectBlog', blog)"
          >
            <h4 class="post-title">{{ blog.title }}</h4>
            <div class="post-meta">
              <span class="views">{{ blog.views }} 阅读</span>
              <span class="likes">{{ blog.likes }} 赞</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="tags">
        <h3>标签云</h3>
        <div class="tag-list">
          <span 
            v-for="tag in [...new Set(blogs.flatMap(blog => blog.tags))]" 
            :key="tag"
            class="tag"
          >
            {{ tag }}
          </span>
        </div>
      </div>
    </div>
  </aside>
</template>

<style scoped>
.sidebar {
  width: 280px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  height: fit-content;
  position: sticky;
  top: 80px;
}

.sidebar-content {
  padding: 24px;
}

.search-box {
  display: flex;
  margin-bottom: 24px;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.search-input {
  flex: 1;
  padding: 12px 16px;
  border: none;
  outline: none;
  font-size: 14px;
}

.search-btn {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border: none;
  padding: 12px 16px;
  cursor: pointer;
  font-size: 16px;
}

.popular-posts h3,
.tags h3 {
  margin: 0 0 16px 0;
  font-size: 18px;
  color: #333;
  border-bottom: 2px solid #f0f0f0;
  padding-bottom: 8px;
}

.post-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.post-item {
  padding: 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid transparent;
}

.post-item:hover {
  background: #f8f9ff;
  border-color: #e0e7ff;
  transform: translateY(-1px);
}

.post-title {
  margin: 0 0 8px 0;
  font-size: 14px;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.post-meta {
  display: flex;
  gap: 12px;
  font-size: 12px;
  color: #666;
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 12px;
  cursor: pointer;
  transition: transform 0.2s;
}

.tag:hover {
  transform: scale(1.05);
}

@media (max-width: 768px) {
  .sidebar {
    width: 100%;
    position: static;
    margin-bottom: 20px;
  }
}
</style>
